package cic;

import java.text.DateFormat;
import java.util.Random;

import junit.framework.Assert;

import org.junit.Test;

public class AcceptanceTest {

	InforMonitor imf;
	InsuranceManager manager;
	Employee emp;
	Employee emp2;
	Employee emp3;
	Employee emp4;
	Insurance insur1;
	Insurance insur2;
	Insurance insur3;
	Insurance insur4;
	
	
	@Test
	public void testInforMonitorCreation() throws Exception {
		// test creation of InforMonitor
		imf = new InforMonitor();
		Assert.assertNotNull(imf);
		imf.addInfor("Initializing system...............done.");
		
		// test creation of InsuranceManager
		manager = new InsuranceManager();
		Assert.assertNotNull(manager);
		
		// test methods of InsuranceManager
		insur1 = new Insurance("TL000001", "Antonio", 1000000, 0);
		insur2 = new Insurance("TL000002", "George", 40000, 2);
		insur3 = new Insurance("TL000003", "Ronaldo", 99990, 0);
		insur4 = new Insurance("TL000004", "Saber", 50000, 3);
		
		manager.addInsurance(insur1);
		manager.addInsurance(insur2);
		manager.addInsurance(insur3);
		manager.addInsurance(insur4);
		
		Assert.assertEquals(insur1, manager.getInsurance("TL000001"));
		Assert.assertEquals(insur2, manager.getInsurance("TL000002"));
		Assert.assertEquals(insur3, manager.getInsurance("TL000003"));
		Assert.assertEquals(insur4, manager.getInsurance("TL000004"));
		
		// test creation of Employee
		emp = new Employee("Michael", "CD0001", Department.CarDamages,	Level.A);
		emp2 = new Employee("Jackson", "CD0002", Department.CarDamages, Level.B);
		emp3 = new Employee("Victoria", "FN0001", Department.Finance, Level.C);
		emp4 = new Employee("Beckham", "CD0003", Department.CarDamages, Level.C);
		
		Assert.assertEquals("Michael", emp.getName());
		Assert.assertEquals("CD0001", emp.getID());
		Assert.assertEquals(Department.CarDamages, emp.getDepartment());
		Assert.assertEquals(Level.A, emp.getLevel());
		
		Assert.assertEquals("Jackson", emp2.getName());
		Assert.assertEquals("CD0002", emp2.getID());
		Assert.assertEquals(Department.CarDamages, emp2.getDepartment());
		Assert.assertEquals(Level.B, emp2.getLevel());
		
		Assert.assertEquals("Victoria", emp3.getName());
		Assert.assertEquals("FN0001", emp3.getID());
		Assert.assertEquals(Department.Finance, emp3.getDepartment());
		Assert.assertEquals(Level.C, emp3.getLevel());
		
		Assert.assertEquals("Beckham", emp4.getName());
		Assert.assertEquals("CD0003", emp4.getID());
		Assert.assertEquals(Department.CarDamages, emp4.getDepartment());
		Assert.assertEquals(Level.C, emp4.getLevel());
		
		Employee handler[] = new Employee[2];
		handler[0] = emp;
		handler[1] = emp2;
		
		// test methods of ReportWindow
		ReportWindow rpw = new ReportWindow();
		rpw.setCustomer("Antonio");
		rpw.setNum("TL000001");
		
		Assert.assertEquals("Antonio", rpw.getCustomer());
		Assert.assertEquals("TL000001", rpw.getNum());
		
		Thread.sleep(3000);
		
		rpw.setSubmitted(true);
		Assert.assertTrue(rpw.isSubmitted());
		
		rpw.hide(true);
		
		// test creation of Claim
		Claim claim = new Claim(rpw.getCustomer(), rpw.getNum());
		imf.addInfor("Customer: " + rpw.getCustomer() + " reported a claim.");
		
		Assert.assertEquals(rpw.getCustomer(), claim.getReporter());
		Assert.assertEquals(rpw.getNum(), claim.getInsurance_num());
		
		emp4.regiserClaim(claim);
		imf.addInfor("Employee: " + emp4.getID() + " " + emp4.getName()
				+ " has registered the claim.");
		
		Assert.assertTrue(claim.isIsregistered());
		
		Random selecthandler = new Random();
		int selected = selecthandler.nextInt(2);
		imf.addInfor("Employee: " + handler[selected].getID() + " "
				+ handler[selected].getName()
				+ " is selected to be handler.");
		
		boolean valid = handler[selected].checkInsurance(manager, claim);
		
		if (valid) {
			
			Assert.assertTrue(valid);
			
			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " is checking the insurance.......");
			imf.addInfor("YES ! The insurance is valid!");

			// Evaluate severity
			handler[selected].evaluate(manager, claim);
			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " is evaluating the severity of the claim......");
			
			if (claim.getCategory() == Category.simple_claim) {
				Assert.assertEquals(Category.simple_claim, claim.getCategory());
				imf.addInfor("It is a simple claim.");
			} else {
				Assert.assertEquals(Category.complex_claim, claim.getCategory());
				imf.addInfor("It is a complex claim.");
			}
			
			// Display the Form window
			FormWindow fmw = new FormWindow();
			Assert.assertNotNull(fmw);
			fmw.setForm("I suffered an big car accident!\nI met an big car accident!\n");
			Thread.sleep(3000);
			fmw.setSubmitted(true);
			Assert.assertTrue(fmw.isSubmitted());
				
			fmw.hide(true);
			
			imf.addInfor("Customer: " + rpw.getCustomer() + " has filled detail forms.");

			// If complex, check damage history
			if (claim.getCategory() == Category.complex_claim) {
				emp.checkHistory(manager, claim);
			}

			// Phone the garage
			handler[selected].phoneGarage();
			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName() + " has phoned the Garage.");

			// Make a decision
			if (handler[selected].getLevel() == Level.A) {
				handler[selected].makeDecision(claim, "OK");
				
				Assert.assertEquals("OK", claim.getDecision());
				
				imf.addInfor("Employee: " + handler[selected].getID() + " "
						+ handler[selected].getName()
						+ " has made a positive decision!...[OK]");
			} else {
				emp.makeDecision(claim, "OK");
				
				Assert.assertEquals("OK", claim.getDecision());
				
				imf.addInfor("Employee: " + emp.getID() + " "
						+ emp.getName()
						+ " has made a positive decision!...[OK]");
			}
			// Pay the money
			emp3.pay(claim);
			imf.addInfor("Employee: " + emp3.getID() + " " + emp3.getName()
					+ " has paid the money.");

			// then write the letter
			Letter letter = null;
			letter = handler[selected]
					.writeLetter("CONGRATULATIONS, YOUR CLAIM HAS BEEN HANDLED!");

			Assert.assertNotNull(letter);
			
			DateFormat df = DateFormat.getDateTimeInstance();

			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " is writing a letter to customer......");

			imf.addInfor("----------------------------------------"
					+ "----Letter--------------------------------------------");
			imf.addInfor("Handler's Name: " + letter.handlername);
			imf.addInfor("Handler's ID: " + letter.handlerid);
			imf.addInfor("DATE TIME: " + df.format(letter.date));
			imf.addInfor("CONTENT: " + letter.content);
			imf.addInfor("----------------------------------------"
					+ "----Letter--------------------------------------------");

			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " has sent the lettet to customer!");
			
			// Display letter window
			String ltcontent = new String("Hi, " + rpw.getCustomer() + "\n" + letter.content
					+ "\n" + "Handler's Name: " + letter.handlername + "\n" +
					"Handler's ID: " + letter.handlerid + "\n" +
					"DATE TIME: " + df.format(letter.date));
			
			new LetterWindow(ltcontent);

		} else {
			Assert.assertFalse(valid);
			
			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " is checking the insurance.");
			imf.addInfor("NO ! The insurance is invalid!");
			// make a decision
			if (handler[selected].getLevel() == Level.A) {
				handler[selected].makeDecision(claim, "NOK");
				
				Assert.assertEquals("NOK", claim.getDecision());
				
				imf.addInfor("Employee: " + handler[selected].getID() + " "
						+ handler[selected].getName()
						+ " has made a negtive decision!...[NOK]");
			} else {
				emp.makeDecision(claim, "NOK");
				
				Assert.assertEquals("NOK", claim.getDecision());
				
				imf.addInfor("Employee: " + emp.getID() + " "
						+ emp.getName()
						+ " has made a negtive decision!...[NOK]");
			}
			// then write the letter
			Letter letter = null;
			letter = handler[selected]
					.writeLetter("SORRY, YOUR CLAIM HAS NOT BEEN ACCEPTED!");
			
			Assert.assertNotNull(letter);

			DateFormat df = DateFormat.getDateTimeInstance();

			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " is writing a letter to customer......");

			imf.addInfor("----------------------------------------"
					+ "----Letter--------------------------------------------");
			imf.addInfor("Handler's Name: " + letter.handlername);
			imf.addInfor("Handler's ID: " + letter.handlerid);
			imf.addInfor("DATE TIME: " + df.format(letter.date));
			imf.addInfor("CONTENT: " + letter.content);
			imf.addInfor("----------------------------------------"
					+ "----Letter--------------------------------------------");

			imf.addInfor("Employee: " + handler[selected].getID() + " "
					+ handler[selected].getName()
					+ " has sent the lettet to customer!");
			
			// Display letter window
			String ltcontent = new String("Hi, " + rpw.getCustomer() + "\n" + letter.content
					+ "\n" + "Handler's Name: " + letter.handlername + "\n" +
					"Handler's ID: " + letter.handlerid + "\n" +
					"DATE TIME: " + df.format(letter.date));
			
			new LetterWindow(ltcontent);
		
		}
		
		imf.addInfor("The processing of the insurance claim is FINISHED!");
		Thread.sleep(3000);
	}


}
